package xyz.bali16.module.search.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.bali16.application.core.enums.ApiEnum;
import xyz.bali16.application.core.model.Result;
import xyz.bali16.module.search.entity.Search;
import xyz.bali16.module.search.model.SearchAddParam;
import xyz.bali16.module.search.model.SearchUpdateParam;
import xyz.bali16.module.search.service.SearchService;


import javax.validation.Valid;
import java.util.List;

import static xyz.bali16.application.core.model.Result.*;
import static xyz.bali16.application.core.model.Result.failure;
import static xyz.bali16.application.core.utils.ResultUtils.errorLog;

/**
 * @author bali2 on 9/3/2023.
 * @version 1.0
 */
@RestController
@RequestMapping(BaseController.API+"search")
@Api(tags = "友情链接接口")
@AllArgsConstructor

public class SearchController extends BaseController{
    public static final Logger logger = LoggerFactory.getLogger(SearchController.class);
    @Autowired
    SearchService searchService;

    @GetMapping
    ////@PreAuthorize("@auth.permission('user:update')")
    @ApiOperation(value = "查询友情链接列表")
    public Result<List<Search>> getList() {
        LambdaQueryWrapper<Search> wrapper = Wrappers.<Search>lambdaQuery()
                .orderByDesc(Search::getOrderNum);
        return success(ApiEnum.GET_SUCCESS, searchService.list(wrapper));
    }

    @PostMapping
    ////@PreAuthorize("@auth.permission('user:add')")
    @ApiOperation("添加友情链接")
    public Result<String> add(@RequestBody @Valid SearchAddParam param) {
        try {
            searchService.addSearch(param);
            return response(ApiEnum.ADD_SUCCESS);
        } catch (Exception e) {
            errorLog(logger, e, "新增友情链接 '{}' 信息失败 {}", param.getName(), e.getMessage());
            return failure(ApiEnum.ADD_ERROR, "新增友情链接失败", e);
        }
    }

    @PutMapping
    ////@PreAuthorize("@auth.permission('user:update')")
    @ApiOperation("修改友情链接")
    public Result<String> update(@RequestBody @Valid SearchUpdateParam param) {
        try {
            searchService.updateSearch(param);
            return response(ApiEnum.UPDATE_SUCCESS);
        } catch (Exception e) {
            errorLog(logger, e, "更新友情链接 '{}' 信息失败 {}", param.getId(), e.getMessage());
            return failure(ApiEnum.UPDATE_ERROR, "更新友情链接失败", e);
        }
    }

    @DeleteMapping("/{searchId}")
    ////@PreAuthorize("@auth.permission('user:delete')")
    @ApiOperation("删除友情链接")
    @ApiImplicitParam(name = "searchId", value = "友情链接id", paramType = "path", required = true)
    public Result<String> deleteSearch(@PathVariable("searchId") String searchId) {
        try {
            searchService.delete(searchId);
            return response(ApiEnum.DELETE_SUCCESS);
        } catch (Exception e) {
            errorLog(logger, e, "删除友情链接 '{}' 失败. {}", searchId, e.getMessage());
            return failure(ApiEnum.DELETE_ERROR, e);
        }
    }

}
